<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.10 Strings</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:48:00 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>How Java Differs from C</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch02_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-10">2.10 Strings</A></h2>

<P CLASS=para>
Strings in Java are <I CLASS=emphasis>not</I> null-terminated arrays of
characters as they are in C.  Instead, they are instances of
the <tt CLASS=literal>java.lang.String</tt> class.  Java strings are
unusual, in that the compiler treats them almost as if they
were primitive types--for example, it automatically creates a
<tt CLASS=literal>String</tt> object when it encounters a double-quoted
constant in the program.  And, the language defines an
operator that operates on <tt CLASS=literal>String</tt> objects--the
<tt CLASS=literal>+</tt> operator for string concatenation.

<P CLASS=para>
An important feature of <tt CLASS=literal>String</tt> objects is that they
are immutable--i.e., there are no methods defined that
allow you to change the contents of a <tt CLASS=literal>String</tt>.
If you need to modify the contents of a <tt CLASS=literal>String</tt>, you
have to create a <tt CLASS=literal>StringBuffer</tt> object from the
<tt CLASS=literal>String</tt> object, modify the contents of the
<tt CLASS=literal>StringBuffer</tt>, and then create a new <tt CLASS=literal>String</tt>
from the contents of the <tt CLASS=literal>StringBuffer</tt>.

<P CLASS=para>
Note that it is moot to ask whether Java strings are
terminated with a NUL character (<tt CLASS=literal>\u0000</tt>) or not.  Java
performs run-time bounds checking on all array and string
accesses, so there is no way to examine the value of any
internal terminator character that appears after the last
character of the string.

<P CLASS=para>
Both the <tt CLASS=literal>String</tt> and <tt CLASS=literal>StringBuffer</tt> classes are
documented in <A HREF="ch25_01.htm">Chapter 25, <i>The java.lang Package</i></A>, and 
you'll find a complete set of methods for string handling and 
manipulation there. Some of the more important <tt CLASS=literal>String</tt> methods are:
<tt CLASS=literal>length()</tt>, <tt CLASS=literal>charAt()</tt>, <tt CLASS=literal>equals()</tt>,
<tt CLASS=literal>compareTo()</tt>, <tt CLASS=literal>indexOf()</tt>,
<tt CLASS=literal>lastIndexOf()</tt>, and <tt CLASS=literal>substring()</tt>.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch02_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Arrays</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
